<?php
include_once("../../php/include_all.php");
$tahun=date('Y');
$tahun2=$tahun-4;

logs('Melihat usulan Desa');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="../../css/bootstrap.min.css" rel="stylesheet">


</head>

<body>
<div class="row-fluid sortable">
	<div class="box span 12 geo-box">
    	<div class="box-header well" ><h2>Usulan Desa</h2></div>
        <div class="box-content">
        <table class="table-awal">
        	<tr><td>Tahun</td><td>&nbsp;</td>
                <td>
                	<select name="tahun" id="tahun" onclick="getlap(this.value)">
                    	<option id="0">-Pilih Tahun-</option>
                        <?php
                       	for($i=$tahun; $i>=$tahun2; $i--){
						?>
                        <option value="<?php echo $i ?>"><?php echo $i ?></option>
                        <?php
                        }
						?>
                    </select>
                </td>
            </tr>
            </table> 
        <div class="accordion" id="accordion2">
            <div class="accordion-group">
                <div class="accordion-heading">
                <a class="btn btn-primary" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo" onclick="emptySet()">Tambah Data</a>
                   
                </div>
        <div id="collapseTwo" class="accordion-body collapse">
          <div class="accordion-inner">
            	<form id="form_usulandesa" class="form-horizontal" name="masterudesa" method="post" action="">
                	<input type="hidden" name="act" value="simpan_udesa">
                    <input type="hidden" id="id_usulankec" name="id_usulankec" value="0">
                    	<table class="table-form">
                        	<tr>
                            	<td>Bidang</td>
                                <td>
                                    <select name="bidang" id="bidang" onChange="bidang()" class="bidang">
                                        <option id="">-Pilih Bidang-</option>
                                        <?php
                                            $bidang = get_datas('select * from mus_bidang_bappeda');
                                            foreach($bidang as $bidang){
                                        ?>
                                        <option value="<?php echo $bidang['id_bdgbapeda']?>"><?php echo $bidang['nama_bdgbapeda']?></option>
                                        <?php
                                        }
                                        ?>
                                    </select>                                
                    			</td>
                            </tr>
                            <tr>
                            	<td>SKPD Tujuan</td>
                                <td>
                                	<select name="skpd" id="skpd">
                                    	<option value="">-Pilih SKPD Tujuan-</option>
                                        
                                    </select>
                                </td>
                            </tr>
                                <tr>
                                    <td>Program dan Kegiatan</td>
                                    <td colspan="5"> 
                                        <a href="#windowProKeg" data-toggle="modal" id="program-kegiatan" name="edit" onclick="detailprogKeg(this)">Pilih program dan Kegiatan</a> (<span id="textProgram"></span>)
                                        <input type="hidden" id="idprogram" name="idprogram" value="" />
                                        <input type="hidden" id="idkegiatan" name="idkegiatan" value="" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>Indikator Hasil Program</td>
                                    <td colspan="5"><textarea  class="input-xxlarge" rows="2" id="ind_hslpro" name="ind_hslpro"></textarea></td>
                                </tr>
                                <tr>
                                    <td>Indikator Keluaran</td>
                                    <td colspan="5"><textarea  class="input-xxlarge" rows="2" id="ind_klr" name="ind_klr"></textarea></td>
                                </tr>
                                <tr>
                                    <td>Indikator Hasil Kegiatan</td>
                                    <td colspan="5"><textarea  class="input-xxlarge" rows="2" id="ind_hslkeg" name="ind_hslkeg"></textarea></td>
                                </tr>
                                <tr>
                                <td>Target Hasil</td><td><input type="text" name="target" id="target" class="span8" /></td>
                    <input type="hidden" id="tahun" name="tahun" value= <? echo "$tahun";?>>
                    <input type='hidden' name='kTambahan' id='kTambahan'>
                            </tr>
                        </table>
                        <div id="kotak">
                        <div id="nyoba">
                        </div>
                        <div id="input0">
                        </div>
                        </div>
                        
                         <a href="javascript:tambah_rincian();"> Klik untuk menambah Rincian</a>
                        <table align="right">
                            <tr>
                                <td></td><td></td><td><input type="button" class="btn btn-primary bt-simpan-udesa" value="Simpan" />
                            </tr>
                        </table>

                </form>
            </div>
        </div>
                <div id="windowProKeg" class="modal hide fade" style="width:800px;left:45%" valign="midle" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <header class="modal-header">
                        <a href="#" class="close" data-dismiss="modal">x</a>
                        <h3>Program dan Kegiatan</h3>
                    </header>
                    <div class="modal-body" id="modal_progKeg">
                        
                    </div>
                </div>
                <div id="showDetail" class="modal hide fade" style="width:1140px;left:33%;" valign="midle" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <header class="modal-header">
                        <a href="#" class="close" data-dismiss="modal">x</a>
                        <h3>Detail Program</h3>
                    </header>
                    <div class="modal-body" id="modal_detail">
                    </div>
               	</div>
        </div>
        </div>
        
            <table class="table table-bordered" align="center" width="100%">
                <thead>
                    <tr>
                        <th width="5%" style="text-align:center">NO</th>
                        <th width="23%" style="text-align:center">PROGRAM</th>
                        <th width="10%" style="text-align:center">HASIL</th>
                        <th width="10%" style="text-align:center">KELUARAN</th>
                        <th width="15%" style="text-align:center">SKPD TUJUAN</th>
                        <th width="10%" style="text-align:center">TAHUN</th>
                        <th width="17%"></th>
                    </tr>
                </thead>
                <tbody id="tbody_udesa">
                <?php
                $udesa = get_datas("select distinct u.id_usulankec, u.id_kegiatan, s.nama_skpd, u.indikator_hasil, u.indikator_program, u.indikator_keluaran, u.target_hasil, u.tahun, u.kegiatan from mus_usulankecamatan u, mus_kegiatan k, mus_skpd s where s.id_skpd=u.id_skpd and (u.id_kegiatan=k.id_keg or u.id_kegiatan=0) order by u.id_usulankec");
                foreach($udesa as $udesa){
					if ($udesa['id_kegiatan'] == 0 ){
						$nama_kegiatan = $udesa['kegiatan'];
					}else{
						$desa = get_data ("select nama_kegiatan from mus_kegiatan where id_keg=".$udesa['id_kegiatan']);
						$nama_kegiatan = $desa['nama_kegiatan'];
					}
                ?>
                <tr>
                    <td style="text-align:center"><?php echo $udesa['id_usulankec']?></td>
                    <td><a href="#showDetail" data-toggle="modal" id="kegiatan" name="kegiatan" onclick="detail(<?php echo $udesa['id_usulankec']?>)"><?php echo $nama_kegiatan ?></a></td>
                    <td><?php echo $udesa['indikator_program']?></td>
                    <td><?php echo $udesa['indikator_keluaran']?></td>
                    <td style="text-align:center"><?php echo $udesa['nama_skpd']?></td>
                    <td style="text-align:center"><?php echo $udesa['tahun']?></td>
                    <td style="text-align:center;">
                        <a href="#collapseTwo" data-toggle="collapse" data-parent="#accordion2" class="btn btn-info btn-small bt-edit-udesa" name="<?php echo $udesa['id_usulankec']; ?>"><i class="icon-edit icon-white"></i>Ubah</a>
                        <a class="btn btn-danger btn-small bt-delete-udesa" name="<?php echo $udesa['id_usulankec']; ?>"><i class="icon-trash icon-white"></i>Hapus</a>
                        </td>
                </tr>
                <?php } ?></tbody>
            </table>
        </div>
    </div>
<script language="javascript" type="text/javascript">
function emptySet(){
	$('#idprogram').val('');
	$('#idkegiatan').val('');
}

$('.bidang').change(function(){
	source = "kecamatan/usulan_desa/skpd_tujuan.php?id="+this.value;
	var coba=$.get(source)
	coba.done(function(data){
		$('#skpd').html(data);
	})
	}
);

function kegiatanTambahan(a){
	var text = a.value;
	console.log(text.length);	
	if(text.length <= 0){
		$('.bt-simpan-keg').addClass('disabled');
	}else{
		$('.bt-simpan-keg').removeClass('disabled');
		var keg = $('#kegiatan').val();
		$('#kTambahan').val(keg);
		$('#textProgram').html(keg);
	}
}

/*$('.bt-simpan-keg').click(function(){
	var keg = $('#kegiatan').val();
	$('#kTambahan').val(keg);
	$('#textProgram').html(keg);
	
});*/
function detailprogKeg (a) {
	var cek = a.name;
	var source = '';
	if (cek == 'edit'){
	var idp = $('#idprogram').val();
	var idk = $('#idkegiatan').val();	
		source = "kecamatan/usulan_desa/prokeg.php?ck=keg&idp="+idp+'&idk='+idk;
	}else{
		source = "kecamatan/usulan_desa/prokeg.php";
	}
	var progkeg = $.get(source);
	progkeg.done(function(dataprokeg){
		$('#modal_progKeg').html(dataprokeg);
	});
	var keg = $('#kTambahan').val();
	if (keg != 0){
		$('#kegiatan').val(keg);
	}
}

function detail(s){
	source = "kecamatan/usulan_desa/detail.php?id_kegiatan="+s;
	var d = $.get (source)
	d.done (function(data){
		$('#modal_detail').html(data)
	});
}
/*$('#desa').change(function(){
	source = this.value;
		$('#iddesa').val(source);
});*/
function getlap(s){
	//var t = $('#tahun').val();
    var src = "kecamatan/usulan_desa/tools.php?cde=lap&thn=" + s;
	var l = $.get(src);
	l.done (function(datas){
		$('#tbody_udesa').html(datas);
	});
}
function getData(a) {
    var radio = $('#optionsRadiosPro_' + a).val();
    sources = "kecamatan/usulan_desa/tools.php?cde=pro&idpro=" + radio;
	var list_keg = $('.item_prog_' + radio).children(".list_keg");
    var keg_open = $(".keg_open");
    keg_open.slideUp();
    keg_open.removeClass('keg_open');
    //    alert(list_keg.html());
    if (!list_keg.hasClass("keg_listed")) {
        var keg = $.get(sources);
		list_keg.addClass('loading');
        list_keg.slideDown();
        keg.done(function(datas) {
            setTimeout(function(){
            list_keg.slideUp();
            list_keg.removeClass('loading')
            setTimeout(function(){
                list_keg.html(datas);
				list_keg.addClass("keg_listed");
                list_keg.slideDown();
                list_keg.addClass("keg_open");                                                                
            },500);
        },2000);                                                        
    })
    }else {
        list_keg.addClass('keg_open');
        list_keg.slideDown();
    }
	$('#idprogram').val(radio);
 }

 
function onclickKegiatan(a,pro){
    var text = $('#textProgram');
    $('#idkegiatan').val(a);
    var src = "kecamatan/usulan_desa/tools.php?cde=text&pro="+pro+"&kgt="+a;
    text.removeClass().addClass('loading');
    var get = $.get(src);
    get.done(function(data){
       text.removeClass('loading');
       text.html(data);
    });
}

counter = 0;

function tambah_rincian(){
	var ccc = $('#kotak').find("input[name='rows[]']");
	var xcounter = ccc.length+1;

	counterNext = counter + 1;
	document.getElementById("input"+counter).innerHTML = "<table align='center'><tr><td><input type='hidden' name='id[]'><input type='text' name='lokasi[]' placeholder='Lokasi'></td><td><input type='text' name='volume[]' placeholder='Volume'></td><td><input type='text' name='apbdkab[]' placeholder='APBD Kab'></td><td><input type='text' name='apbdprov[]' placeholder='APBD Prov'></td><td><input type='text' name='apbn[]' placeholder='APBN'></td><td><input type='text' name='prioritas[]' value="+xcounter+" style='width:50px'></td></td><input type='hidden' name='rows[]' value='"+xcounter+"'></tr></table><div id=\"input"+counterNext+"\"></div>";
	counter++;
}

</script>



<script>
var urls = 'kecamatan/usulan_desa/aksi_udesa.php';
$('.bt-simpan-udesa').click(function(){
	var btn = $(this);
	var load = $('#loader');
	var rslt = $('#result');
	load.addClass('spinner pull-left');
	btn.removeClass('btn-primary').addClass('btn-info');
	
	var form =$('#form_usulandesa');
	var data = form.serializeArray();
	var post = $.post(urls,data);
	post.done(function(res){
		console.log(res);
		var result = res.split('__');
		if(result.length==3){
		 	
			if(result[0]=='success'){
				rslt.html('<font color="green">Tersimpan</font>');
				setTimeout(function(){
					rslt.html('');
				},1500);
				
				var tbody = $('#tbody_udesa');
				tbody.html(result[2]);
				init();
				document.getElementById('id_usulankec').value='';
				document.getElementById('bidang').value='';
				document.getElementById('skpd').value='';
				document.getElementById('idprogram').value='';
				document.getElementById('idkegiatan').value='';
				document.getElementById('ind_hslpro').value='';
				document.getElementById('ind_klr').value='';
				document.getElementById('ind_hslkeg').value='';
				document.getElementById('target').value='';
				document.getElementById('tahun').value='';
				$('#idprogram').val('');
				$('#idkegiatan').val('');
				$('#textProgram').html('');
				var ktk = $('#kotak');
				ktk.html("<div id='nyoba'></div><div id='input0'></div>");
			}else{
				rslt.html('<font color="red">'+res+'</font>');
			}
		}else{
			rslt.html('<font color="red">'+res+'</font>');
		}
		load.removeClass();
		btn.removeClass('btn-info').addClass('btn-primary');
		var col =	$('#collapseTwo');
		col.removeClass('in');
		col.css('height','0');

	});
});

function init(){
	$('.bt-delete-udesa').click(function(){
		var btn  = $(this);
		var pid  = this.name;
		var load = $(this);
		load.html('<div class="spinner"></div>');
		bootbox.confirm("Anda akan menghapus kegiatan ini?", function(result) {
			if(result==true){
				if(pid!=0){
					var post = $.post(urls,{act:"del_udesa",id_usulankec:pid});
					post.done(function(res){
						
						var hasil = res.split('__');
						if(hasil.length == 2){
							if(hasil[0] == 'success'){
								var tbody = $('#tbody_udesa');
								tbody.html(hasil[1]);
								
								init();
							}
						}
					});
					load.html('<i class="icon-trash icon-white"></i>');
				}
			}else{
				load.html('<i class="icon-trash icon-white"></i>');
			}
		});
	});
	$('.bt-edit-udesa').click(function(){
		var id_usulankec = this.name;
		var form = $('#form_usulandesa');
		var post = $.post(urls,{act:'edit_udesa',id_usulankec:id_usulankec});
		post.done(function(res){
		console.log(res);
			var value = res.split('__');
			$('#id_usulankec').val(value[0]);
			//$('#iddesa').val(value[1]);
			form.find('select[name="bidang"]').val(value[7]);
			form.find('select[name="skpd"]').val(value[1]);
			form.find('select[name="skpd"]').html(value[11]);
			form.find('textarea[name="ind_hslpro"]').val(value[3]);
			form.find('textarea[name="ind_klr"]').val(value[4]);
			form.find('textarea[name="ind_hslkeg"]').val(value[5]);
			form.find('input[name="target"]').val(value[6]);
			$('#kTambahan').val(value[9]);
			$('#kegiatan').val(value[9]);
			$('#nyoba').html(value[12]);
			$('#idprogram').val(value[10]);
			$('#idkegiatan').val(value[8]);
			var klain = $('#kTambahan').val();
			if( klain != 0){
				$('#textProgram').html(value[9]);
			}else{
				$('#textProgram').html(value[13]);
			}
				//document.getElementById('judul').value='';
				//document.getElementById('pengantar').value='';
				//document.getElementById('isi').value='';
				//$('#modalwin').modal('hide');
		});
	});
}

init();

</script>
</body>
</html>
